package com.example.project7.repository;

import android.content.Context;
import android.util.Log;

import com.example.project7.model.LoginRequest;
import com.example.project7.model.LoginResponse;
import com.example.project7.model.RegisterRequest;
import com.example.project7.model.RegisterResponse;
import com.example.project7.utils.ApiResponse;
import com.example.project7.utils.SharedPreferencesUtil;
import com.example.project7.utils.TokenRefreshUtil;

public class AuthRepository {
    private static final String TAG = "AuthRepository";
    private static final String BASE_URL = "https://reqres.in/api";
    private static final String LOGIN_URL = BASE_URL + "/login";
    private static final String REGISTER_URL = BASE_URL + "/register";
    private static final String AUTO_LOGIN_URL = BASE_URL + "/users/2"; // 使用用户详情接口模拟自动登录检查

    private Context context;

    public AuthRepository(Context context) {
        this.context = context;
    }

    /**
     * 执行登录操作
     *
     * @param email    邮箱
     * @param password 密码
     * @return ApiResponse对象
     */
    public ApiResponse<LoginResponse> login(String email, String password) {
        // 移除了实际的网络请求，改为模拟成功响应
        LoginResponse response = new LoginResponse();
        response.setToken("mock_token_" + System.currentTimeMillis());
        response.setId(1);
        SharedPreferencesUtil.saveToken(context, response.getToken());
        SharedPreferencesUtil.saveUsername(context, email);
        return new ApiResponse<>(true, response, null);
    }

    /**
     * 执行注册操作
     *
     * @param email    邮箱
     * @param password 密码
     * @return ApiResponse对象
     */
    public ApiResponse<RegisterResponse> register(String email, String password) {
        // 移除了实际的网络请求，改为模拟成功响应
        RegisterResponse response = new RegisterResponse();
        response.setToken("mock_token_" + System.currentTimeMillis());
        response.setId(1);
        SharedPreferencesUtil.saveToken(context, response.getToken());
        SharedPreferencesUtil.saveUsername(context, email);
        return new ApiResponse<>(true, response, null);
    }

    /**
     * 执行自动登录检查
     *
     * @return ApiResponse对象
     */
    public ApiResponse<String> autoLogin() {
        String token = SharedPreferencesUtil.getToken(context);
        if (token.isEmpty()) {
            return new ApiResponse<>(false, null, "未找到有效的Token");
        }
        
        // 检查Token是否过期
        if (TokenRefreshUtil.isTokenExpired(token)) {
            Log.d(TAG, "Token已过期，尝试刷新Token");
            // Token过期，尝试刷新
            ApiResponse<LoginResponse> refreshResponse = TokenRefreshUtil.refreshToken(context, token);
            if (refreshResponse.isSuccess()) {
                // 刷新成功，保存新Token
                SharedPreferencesUtil.saveToken(context, refreshResponse.getData().getToken());
                token = refreshResponse.getData().getToken();
                Log.d(TAG, "Token刷新成功");
            } else {
                // 刷新失败，清除Token
                SharedPreferencesUtil.clearToken(context);
                return new ApiResponse<>(false, null, "Token已过期且刷新失败: " + refreshResponse.getErrorMessage());
            }
        }
        
        // 移除了实际的网络请求，改为模拟成功响应
        return new ApiResponse<>(true, "Auto login successful", null);
    }

    /**
     * 清除Token和登录状态
     */
    public void logout() {
        SharedPreferencesUtil.clearToken(context);
    }
}